<?php

namespace App\Services;

use App\CodeResponse;
use App\Exceptions\BusinessException;
use App\Models\User;
use App\Notifications\VerificationCode;
use Carbon\Carbon;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\Cache;

class UserServices extends BaseServices
{
    /**
     * 根据用户名获取用户    获取没有被软删除的用户
     *
     * @param $username
     *
     * @return User|null|Model
     */
    public function getByUsername($username)
    {
        return User::query()->where("username", $username)->where('deleted', 0)->first();
    }

    /**
     * 根据手机号获取用户    获取没有被软删除的用户
     *
     * @param $mobile
     *
     * @return User|null|Model
     */
    public function getByMobile($mobile)
    {
        return User::query()->where("mobile", $mobile)->where('deleted', 0)->first();
    }

    /**
     * 验证手机号发送验证码是否达到限制条数
     *
     * @param $mobile
     *
     * @return bool
     */
    public function checkMobileSendCaptchaCount($mobile)
    {
        $countKey = "register_captcha_count_$mobile";
        if (Cache::has($countKey)) {
            $count = Cache::increment($countKey);
            if ($count > 10) {
                return false;
            }
        } else {
            Cache::put($countKey, 1, Carbon::tomorrow()->diffInSeconds(now()));
        }

        return true;
    }

    /**
     * 发送验证码
     *
     * @param $mobile
     *
     * @param $code
     *
     * @return array|void
     */
    public function sendCaptchaMsg($mobile, $code)
    {
        if (app()->environment('testing')) {
            return;
        }

        return $this->sendTemplateSMS($mobile, $code);
    }

    /**
     * 容联云短信发送平台
     *
     * @param $to
     * @param $datas
     *
     * @return array|void
     */
    function sendTemplateSMS($to, $datas)
    {
        include_once("./public/Sms/SmsSDK.php");
        //模板id
        $tempId = 1;
        //主帐号
        $accountSid = '8aaf0708732220a601739809d4d7340a';
        //主帐号Token
        $accountToken = 'de8d287366884991becfb7e8d01c573d';
        //应用Id
        $appId = '8aaf0708732220a601739809d59d3410';
        //请求地址，格式如下，不需要写https://,默认为：app.cloopen.com'
        $serverIP = 'app.cloopen.com';
        //请求端口 默认为：8883
        $serverPort = '8883';
        //REST版本号 默认为：2013-12-26
        $softVersion = '2013-12-26';
        // 初始化REST SDK
        $rest = new \REST($serverIP, $serverPort, $softVersion);
        $rest->setAccount($accountSid, $accountToken);
        $rest->setAppId($appId);
        // 发送模板短信
        $result = $rest->sendTemplateSMS($to, $datas, $tempId);
        if ($result == null) {
            return;
        }
        if ($result->statusCode != 0) {
            //TODO 添加错误处理逻辑
        } else {
            // 获取返回信息
            //TODO 添加成功处理逻辑
            return true;
        }
    }

    /**
     * 验证短信验证码
     *
     * @param $mobile
     * @param $code
     *
     * @return bool
     * @throws BusinessException
     */
    public function checkCaptcha($mobile, $code)
    {
        $key    = "register_captcha_$mobile";
        $isPass = $code == Cache::get($key);
        if ($isPass) {
            Cache::forget($key);
        } else {
            throw new BusinessException(CodeResponse::AUTH_CAPTCHA_UNMATCH);
        }
        return $isPass;
    }

    /**
     * 生成验证码
     *
     * @param $mobile
     *
     * @return int
     */
    public function setCaptcha($mobile)
    {
        //  随机生成6位验证码
        $code = rand(100000, 999999);
        //  保存手机号和验证码的关系
        Cache::put("register_captcha_$mobile", $code, 600);

        return $code;
    }

}





















